package com.uhylab.uhuvinetest.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.activity.UHInformationActivity;
import com.uhylab.uhuvinetest.activity.UHManagerActivity;
import com.uhylab.uhuvinetest.activity.UHMediaPlayerActivity;
import com.uhylab.uhuvinetest.activity.UHNewsActivity;
import com.uhylab.uhuvinetest.activity.UHPhotoActivity;
import com.uhylab.uhuvinetest.activity.UHScanDeviceActivity;
import com.uhylab.uhuvinetest.activity.UHTrendActivity;
import com.uhylab.uhuvinetest.base.UHBaseFragment;
import com.uhylab.uhuvinetest.dao.CloudHistory;
import com.uhylab.uhuvinetest.dao.CloudHistoryDao;
import com.uhylab.uhuvinetest.dao.DaoSingleton;
import com.uhylab.uhuvinetest.utils.Shareutils;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;
import com.uhylab.uhuvinetest.view.RoundImageView;
import com.uhylab.uhuvinetest.view.WhewView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by leige07 .
 * Time on 2016/5/17 0017.
 * Description
 * 修改时间 19.26  6/1
 * 扩大波纹范围 删除无用的handler
 */
public class UHNewHomeFragment extends UHBaseFragment implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private RoundImageView iv_camera_newhome,iv_u2_newhome;
    private ImageView iv_trangle_direct1, iv_trangle_direct3, iv_trangle_direct4, iv_trangle_direct2, iv_trangle_direct5;
    private ImageView iv_main_manager;
    private ImageView iv_main_trend;
    private ImageView iv_main_guide;
    private ImageView iv_main_tips;
    private ImageView main_reminder;
    private ImageView main_more;
    private WhewView wv;
    private TextView mTextView;
    private go2Two mGo2Two;

    private ViewPager check_viewpager;

    private CloudHistoryDao cloudHistoryDao;
    private Thread mThread;
    private boolean threadStart = false;

    private String date;

    private List<View> fragments;

    //判断此时是U1还是U2
    private Boolean isU1 = true;

    //界面U1和U2
    private View U1;
    private View U2;

    //向左向右的箭头
    private ImageView vp_left_u1, vp_right_u1;
    private ImageView vp_left_u2, vp_right_u2;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mGo2Two = (go2Two) context;
    }

    @Override
    public void initData() {
        iv_camera_newhome.setOnClickListener(this);
        iv_main_manager.setOnClickListener(this);
        iv_main_trend.setOnClickListener(this);
        iv_main_guide.setOnClickListener(this);
        iv_main_tips.setOnClickListener(this);
        main_more.setOnClickListener(this);
        main_reminder.setOnClickListener(this);

        mTextView.setOnClickListener(this);

        iv_u2_newhome.setOnClickListener(this);
        vp_left_u1.setOnClickListener(this);
        vp_left_u2.setOnClickListener(this);
        vp_right_u1.setOnClickListener(this);
        vp_right_u2.setOnClickListener(this);

        check_viewpager.addOnPageChangeListener(this);

        // 执行动画
        if (isU1){
            wv.start();
        }

        fragments = new ArrayList<View>();

        fragments.add(U1);
        fragments.add(U2);

        check_viewpager.setAdapter(new CheckFragmentAdapter());
    }

    @Override
    public void onResume() {
        super.onResume();
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {

                cloudHistoryDao = DaoSingleton.getInstance().getCloudHistoryDao();
                final List<CloudHistory> cloudHistories = cloudHistoryDao.loadAll();
                if (cloudHistories != null && cloudHistories.size() > 0) {
                    if (SharedPreUtils.getBoolean(mActivity, "is_Login", false))
                        mTextView.post(new Runnable() {
                            @Override
                            public void run() {
                                date = cloudHistories.get(0).getCreatetime();
                                if (date != null) {
                                    mTextView.setText("您上次的检测时间为" + date);
                                    String ascorbicacid_status = cloudHistories.get(0).getAscorbicacid_status();
                                    String bilirubin_status = cloudHistories.get(0).getBilirubin_status();
                                    String blood_status = cloudHistories.get(0).getBlood_status();
                                    String glucose_status = cloudHistories.get(0).getGlucose_status();
                                    String gravity_status = cloudHistories.get(0).getGravity_status();
                                    String ketones_status = cloudHistories.get(0).getKetones_status();
                                    String leukocytes_status = cloudHistories.get(0).getLeukocytes_status();
                                    String nitrite_status = cloudHistories.get(0).getNitrite_status();
                                    String ph_status = cloudHistories.get(0).getPh_status();
                                    String protein_status = cloudHistories.get(0).getProtein_status();
                                    String urobilinogen_status = cloudHistories.get(0).getUrobilinogen_status();
                                    ArrayList one = new ArrayList();
                                    ArrayList two = new ArrayList();
                                    String[] strings = {
                                            ascorbicacid_status, bilirubin_status, blood_status, glucose_status
                                            , gravity_status, ketones_status, leukocytes_status, nitrite_status
                                            , ph_status, protein_status, urobilinogen_status
                                    };
                                    for (String string : strings) {
                                        if (string != null) {
                                            onType(string, one, two);
                                        }
                                    }
                                    if (two.size() >= 2 || (one.size() >= 3 && two.size() >= 1)) {
                                        iv_trangle_direct5.setVisibility(View.VISIBLE);
                                        iv_trangle_direct3.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct2.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct4.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct1.setVisibility(View.INVISIBLE);
                                        //wv.setcolor3();
                                    } else if ((two.size() < 2 && two.size() >= 1) || (one.size() >= 3 && two.size() == 0)) {
                                        iv_trangle_direct3.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct4.setVisibility(View.VISIBLE);
                                        iv_trangle_direct2.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct5.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct1.setVisibility(View.INVISIBLE);
                                        //wv.setcolor2();
                                    } else if (one.size() < 2 && two.size() < 1) {
                                        iv_trangle_direct3.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct2.setVisibility(View.VISIBLE);
                                        iv_trangle_direct5.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct4.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct1.setVisibility(View.INVISIBLE);
                                        //wv.setcolor1();
                                    } else if (one.size() < 1 && two.size() < 1) {
                                        iv_trangle_direct3.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct2.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct5.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct4.setVisibility(View.INVISIBLE);
                                        iv_trangle_direct1.setVisibility(View.VISIBLE);
                                        //wv.setColor0();
                                    }
                                    one.clear();
                                    two.clear();
                                }
                            }
                        });
                }
            }
        });

        main_more.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!threadStart) {
                    mThread.start();
                    threadStart = true;
                }
            }
        }, 1000);

    }


    private void onType(String ascorbicacid_status, ArrayList one, ArrayList two) {
        if (ascorbicacid_status.equals("0")) {
        } else if (ascorbicacid_status.equals("1")) {
            one.add("1");
        } else if (ascorbicacid_status.equals("2")) {
            two.add("2");
        }
    }


    @Override
    protected void initDestrory() {

    }

    @Override
    protected void initView() {
        iv_camera_newhome = (RoundImageView) U1.findViewById(R.id.iv_camera_newhome);
        wv = (WhewView) U1.findViewById(R.id.wv);
        iv_u2_newhome = (RoundImageView) U2.findViewById(R.id.iv_u2_newhome);
        iv_trangle_direct1 = (ImageView) view.findViewById(R.id.iv_trangle_direct1);
        iv_trangle_direct2 = (ImageView) view.findViewById(R.id.iv_trangle_direct2);
        iv_trangle_direct3 = (ImageView) view.findViewById(R.id.iv_trangle_direct3);
        iv_trangle_direct4 = (ImageView) view.findViewById(R.id.iv_trangle_direct4);
        iv_trangle_direct5 = (ImageView) view.findViewById(R.id.iv_trangle_direct5);
        mTextView = (TextView) view.findViewById(R.id.test_time);
        iv_main_manager = (ImageView) view.findViewById(R.id.iv_main_manager);
        iv_main_trend = (ImageView) view.findViewById(R.id.iv_main_trend);
        iv_main_guide = (ImageView) view.findViewById(R.id.iv_main_guide);
        iv_main_tips = (ImageView) view.findViewById(R.id.iv_main_tips);
        main_more = (ImageView) view.findViewById(R.id.main_more);
        main_reminder = (ImageView) view.findViewById(R.id.main_reminder);
        check_viewpager = (ViewPager) view.findViewById(R.id.check_viewpager);
        vp_left_u1 = (ImageView) U1.findViewById(R.id.vp_left_u1);
        vp_right_u1 = (ImageView) U1.findViewById(R.id.vp_right_u1);
        vp_left_u2 = (ImageView) U2.findViewById(R.id.vp_left_u2);
        vp_right_u2 = (ImageView) U2.findViewById(R.id.vp_right_u2);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = View.inflate(mActivity, R.layout.fragment_newhome, null);
        U1 = View.inflate(mActivity,R.layout.u1_viewpager,null);
        U2 = View.inflate(mActivity,R.layout.u2_viewpager,null);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_more:
                Shareutils.showShare();
                break;
            case R.id.iv_camera_newhome:
                Intent intent = new Intent(getContext(), UHPhotoActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_main_manager:
                Intent managerIntent = new Intent(getContext(), UHManagerActivity.class);
                startActivity(managerIntent);
                break;
            case R.id.iv_main_trend:
                Intent trend = new Intent(getContext(), UHTrendActivity.class);
                startActivity(trend);
                break;
            case R.id.iv_main_guide:
                Intent guide = new Intent(getContext(), UHMediaPlayerActivity.class);
                startActivity(guide);
                break;
            case R.id.iv_main_tips:
                Intent news = new Intent(getContext(), UHNewsActivity.class);
                startActivity(news);
                break;
            case R.id.test_time:
                mGo2Two.toTwo(date);
                break;
            case R.id.main_reminder:
                Intent informationCenter = new Intent(getContext(), UHInformationActivity.class);
                startActivity(informationCenter);
                break;
            case R.id.iv_u2_newhome:
                //进入到U2操作界面
                Intent U2Intent = new Intent(getContext(),UHScanDeviceActivity.class) ;
                startActivity(U2Intent);
                break;
            case R.id.vp_left_u1:
                check_viewpager.setCurrentItem(1,false);
                break;
            case R.id.vp_right_u1:
                check_viewpager.setCurrentItem(1);
                break;
            case R.id.vp_left_u2:
                check_viewpager.setCurrentItem(0);
                break;
            case R.id.vp_right_u2:
                check_viewpager.setCurrentItem(0,false);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



    public interface go2Two {
        void toTwo(String s);
    }

    class CheckFragmentAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
           return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(fragments.get(position));
            return fragments.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(fragments.get(position));
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
